<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>选择人员</title>
</head>
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0 minimal-ui"/>
<%@ include file="/WEB-INF/jsp/kt/common/inc.jsp" %>
<script type="text/javascript" src="${_resources}js/jquery.ztree.core.js"></script>
<link rel="stylesheet" href="${_resources}css/zTreeStyle.css" type="text/css">
<link href='${_resources}css/style.css' type='text/css' rel='stylesheet' />
<link rel="stylesheet" href="${_resources}css/reset.css" type="text/css" />
<%-- <script type="text/javascript" src="${_resources}js/zUI.js"></script> --%>
<style type="text/css">

.ztree li span.button.ico_open,.ztree li span.button.ico_docu,.ztree li span.button.ico_close{width:0px}
    #main-left{border:0px}
    .selectPersonnel_select2 input{display:none;
filter:alpha(opacity=0);  /* ie 有效*/
	-moz-opacity:0; /* Firefox  有效*/
	opacity: 0; /* 通用，其他浏览器  有效*/}
	.ztree{color:#999}
	.ztree li span{color:#999}
	.selectPersonnel{width:680px;
            height:180px;
            overflow:hidden;}
        .main_left{width:170px;overflow-x: auto;}
        .zUIpanelScrollBox,.zUIpanelScrollBar{
			width:5px;
			top:4px;
			right:2px;
			border-radius:5px;
			display:block;
		}
		.zUIpanelScrollBox{
			background:#c5c5c5;opacity:0.3;
			filter:alpha(opacity=30);
		}
		.zUIpanelScrollBar{
			background:#c5c5c5;opacity:0.8;
			filter:alpha(opacity=80);
		}
	.main_right {
    position: absolute;
    left: 170px; 
    top: 0;
    bottom: 0;
    z-index: 1000;
    height: 100%;
    width: 460px;
}
.main_left #treeDemo{overflow-x: hidden;}
.ztree {margin-left: 12px;}
.main_right{border-left:0px}
.selectPersonnel_select2{margin:0;text-align: center;}
.selectPersonnel_select1 p{margin: 8px;}
.selectPersonnel_div4{margin-left:0;}
.selectPersonnel_select1{margin-left:0; widht:330px;}
.selectPersonnel_select1 p{margin: 5px;}
.selectPersonnel_select2 p{margin: 3px;}
.selectPersonnel_div1{margin-left: 0px;text-align: center;}
.selectPersonnel_div2{width: 308px;}
.selectPersonnel_div4{width: 140px;}
.selectPersonnel_div1{font-size: 16px;}
.ztree li a,.ztree .level_0 a.curSelectedNode{padding: 3px 3px 0 0;}
</style>
<script type="text/javascript">
if (!Array.prototype.indexOf){  
    Array.prototype.indexOf = function(elt /*, from*/){  
    var len = this.length >>> 0;  
    var from = Number(arguments[1]) || 0;  
    from = (from < 0)  
         ? Math.ceil(from)  
         : Math.floor(from);  
    if (from < 0)  
      from += len;  
    for (; from < len; from++)  
    {  
      if (from in this &&  
          this[from] === elt)  
        return from;  
    }  
    return -1;  
  };  
}
var userListHtml="";
var userIdList="";
var userNameList="";
var selectUserList = [];
var managerIdSplit = "";
var checkedHtml = "";
var inputId = "${inputId}";
$(function (){
	var managerIds = "${managerId}";
	managerIdSplit = managerIds.split(",");
	$("#close").click(function (){
		parent.closeWin();
	});
	
	$("#allDelFun").click(function (){
		userListHtml="";
		userIdList="";
		userNameList="";
		$("#userList").html(userListHtml);
	});
	
	$("#delFun").click(function (){
		parent.closeWin();
	});
	
	$("#searchValue").keydown(function(event) {//给输入框绑定按键事件
	    if(event.keyCode == "13") {//判断如果按下的是回车键则执行下面的代码
	    	search();
	  	}
	})
	
	$("#search").click(function (){
		search();
	});
	
	if("${inputId}"=="chargeInDeptAdd"){
		$("#selectPersonnel1").hide();
		// 变两列
		$("#main-right").css("left","0px");
		$("#selectPersonnel2").css("border-left","none");
	}
});

function search(value){
	$("#selectUser").empty();
	//获取选择的目录：
	var path="${_baseUrl}/userController/selectDepartUser";
 	 $.ajax({    
		type: "post",    
		async: false,    
		url:path,   
		data:{
			"value":value,
			"departId":"${departId}"
		},    
		dataType:"json",    
		success: function (data) { 
			var obj = JSON.stringify(data);
		    innerHtml="";
		    if(obj=="[]"){
				innerHtml+=""; 
			}else{  
				for(var i=0;i<data.length;i++){	
					if(selectUserList.toString().indexOf(data[i].userId) !=-1){
						var html = "<p class=\"pick\" onclick='dj(this)' id='p_"+data[i].userId+"'><input type='hidden' id='"+data[i].userId+"' name='"+data[i].userId+"' value='"+data[i].userId+"'/><input type='hidden' id='userName' name='userName' value='"+data[i].userName+"'/>"+data[i].userName+"</p>";
				 		$("#selectUser").append(html);
					}
					if(managerIdSplit.indexOf(data[i].id) >= 0){
						innerHtml+="<p class=\"pick\" onclick='dj(this)'><input class='chk_all' checked='checked' name='"+data[i].id+"_chk' id='"+data[i].id+"' value='"+data[i].userName+"' type='radio' onClick=\"addSelectUser('"+data[i].userId+"','"+data[i].userName+"','"+data[i].id+"');\">"+data[i].userName+"</p>";
						//判断如果右侧已存在，则不需要在加入
						
					}else{
						if("${team_id}".indexOf(data[i].id) < 0 ){
							innerHtml+="<p onclick='dj(this)'><input class='chk_all' name='"+data[i].id+"_chk' id='"+data[i].id+"' value='"+data[i].userName+"' type='radio' onClick=\"addSelectUser('"+data[i].userId+"','"+data[i].userName+"','"+data[i].id+"');\">"+data[i].userName+"</p>";
						}
					}
				}
			}                 
		    $("#userList").children("p").remove();
			$("#userList").append(innerHtml); 
		}	
	});
}
var setting = {	
	view: {
		showLine: true,
		selectedMulti: false,
		addDiyDom: addDiyDom,
		showIcon: showIconForTree
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		onNodeCreated: this.onNodeCreated,				
		onClick: onClick		
	}

};
var zNodes = eval("${zNodes}");

$(function (){
	var tree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
	
	var nodes = tree.transformToArray(tree.getNodes()); 
	for(var lt=0;lt<nodes.length;lt++){
		var node = nodes[lt];
		//var nodeName = node.name;
		
		//if(nodeName == "所有部门"){
		if(lt==0){
			tree.setting.callback.onClick(null, tree.setting.treeId, node);//触发函数
		}
		
		//}
	}
	
	if($(".zzjgs_sorll").size()>0){
		$('.zzjgs_sorll').jScrollPane();
	}
	if($(".mulu_jieg").size()>0){
		$('.mulu_jieg').jScrollPane();
	}
	
});

function addDiyDom(treeId, treeNode) {
	var liObj = $("#" + treeNode.tId);
	var aObj = $("#" + treeNode.tId + "_span");
	liObj.addClass("level_"+treeNode.level);
	aObj.addClass("le_"+treeNode.level);
};

 if (!Array.prototype.indexOf){  
        Array.prototype.indexOf = function(elt /*, from*/){  
        var len = this.length >>> 0;  
        var from = Number(arguments[1]) || 0;  
        from = (from < 0)  
             ? Math.ceil(from)  
             : Math.floor(from);  
        if (from < 0)  
          from += len;  
        for (; from < len; from++)  
        {  
          if (from in this &&  
              this[from] === elt)  
            return from;  
        }  
        return -1;  
      };  
    }  
 function showIconForTree(treeId, treeNode) {
		return !treeNode.isParent;
	};
function onClick(e,treeId, treeNode){
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.expandNode(treeNode);
	var path = "";
	departId = treeNode.id ;
	if("${set_document_departId}" != ''){
		path="${_baseUrl}/userController/selectDepartUser?set_document_departId=${set_document_departId}";
	}else if("${source}" != ''){
		path="${_baseUrl}/userController/selectDepartUser?source=${source}";
	}else if("${departId}" != ''){
		path="${_baseUrl}/userController/selectDepartUser";
		departId = "${departId}";
	}else if("${inputId}" == 'chargeInDeptAdd'){
		path="${_baseUrl}/userController/selectDepartUser";
	}else{
		path="${_baseUrl}/userController/selectDepartUser";
	}
 	 $.ajax({    
		type: "POST",    
		async: false,    
		url:path,   
		data:{
			"departId":departId    
		},    
		dataType:"json",    
		success: function (data) {   
		    var obj = JSON.stringify(data);
		    innerHtml="";
		    if(obj=="[]"){
				innerHtml+=""; 
			}else{  
				for(var i=0;i<data.length;i++){	
					
					if("${inputId}" == 'chargeInDeptAdd' && data[i].departId != undefined || data[i].userName == "超级管理员"){
						continue;
					}
					if(managerIdSplit.indexOf(data[i].id) >= 0){
						
						innerHtml+="<p class=\"pick\" onclick='dj(this)'><input class='chk_all' checked='checked' name='"+treeNode.id+"_chk' id='"+data[i].id+"' value='"+data[i].userName+"' type='radio' onClick=\"addSelectUser('"+data[i].userId+"','"+data[i].userName+"','"+data[i].id+"');\">"+data[i].userName+"</p>";
						//判断如果右侧已存在，则不需要在加入
						var flag = true;
						for(var ii=0;ii < selectUserList.length;ii++){
				 			var obj = JSON.parse(selectUserList[ii]);
				 			if(data[i].id == obj.id){
				 				flag = false;
				 			}
				 		}
						
						if("${team_id}".indexOf(data[i].id) >= 0){
							flag = false;
						}	
						if(flag){
							//selectUserList = [];
							selectUserList.push("{\"userId\":\""+data[i].userId+"\",\"userName\":\""+data[i].userName+"\",\"id\":\""+data[i].id+"\"}");
					 		var html = "<p class=\"pick\" onclick='dj(this)' id='p_"+data[i].id+"'><input type='hidden' id='userId' name='userId' value='"+data[i].userId+"'/><input type='hidden' id='userName' name='userName' value='"+data[i].userName+"'/><input name='"+data[i].id+"_chk' id='"+data[i].id+"' value='"+data[i].userName+"' type='checkBox' onClick=\"delSelectUser('"+data[i].userId+"','"+data[i].userName+"','"+data[i].id+"',this);\">"+data[i].userName+"</p>";
					 		$("#selectUser").append(html);
						}
						
					}else{
						
						if("${team_id}".indexOf(data[i].id) < 0 ){
							innerHtml+="<p onclick='dj(this)'><input class='chk_all' name='"+treeNode.id+"_chk' id='"+data[i].id+"' value='"+data[i].userName+"' type='radio' onClick=\"addSelectUser('"+data[i].userId+"','"+data[i].userName+"','"+data[i].id+"');\">"+data[i].userName+"</p>";
						}
					}
				}
			}                 
		    $("#userList").children("p").remove();
			$("#userList").append(innerHtml); 
			
			//默认选中右侧已有人员
			for(var i=0;i<selectUserList.length;i++){
	 			var obj = JSON.parse(selectUserList[i]);
	 			$("#"+obj.id).attr("checked",true);
	 		}
			
		}   
	});
}

function disabledNode(e) {
	var zTree = $.fn.zTree.getZTreeObj("tree"),
	disabled = e.data.disabled,
	nodes = zTree.getSelectedNodes();
	if (nodes.length == 0) {
		layer.msg("请先选择一个节点");
	}

	for (var i=0, l=nodes.length; i<l; i++) {
		zTree.setChkDisabled(nodes[i], disabled);
	}
}

/* 选择一个节点添加到右侧 */
	var inputId = "single_16fcf2c7daf142c284eea23499ecee2b_task4";
	function addSelectUser(userId,userName,id){
 	//$("#selectUser").html(""); 
 	//修改Bug[3328] [#3328] 程序错误，部门管理人不是必填项，但修改时无法取消勾选
 	 $('.chk_all').each(function(){
        	$(this).change(function() { 
        		 if($(this).attr('checked')){
 	                $('.chk_all').removeAttr('checked');
 	                $(this).attr('checked','checked');
 	            }
        	}); 
	});
 	
 	if('${inputId}' == 'manager' ){
 		if($("#"+id).attr("checked")){
	 		selectUserList.push("{\"userId\":\""+userId+"\",\"userName\":\""+userName+"\",\"id\":\""+id+"\"}");
	 		var html = "<p class=\"pick\" id='p_"+id+"'><input type='hidden' id='userId' name='userId' value='"+userId+"'/><input type='hidden' id='userName' name='userName' value='"+userName+"'/><input name='"+id+"_chk' id='"+id+"' value='"+userName+"' type='checkBox' onClick=\"delSelectUser('"+userId+"','"+userName+"','"+id+"',this);\">"+userName+"</p>";
	 		$("#selectUser").append(html); 
	 		managerIdSplit.push(id);
		} else{
			selectUserList.length=0;
		} 
 	} else{
 		if($("#"+id+":checked").val() != null){
 			if('${inputId}' == "project_manager" || '${inputId}' == 'charge' || '${inputId}' == 'chargeInDeptAdd' || '${inputId}' == 'single_implPerson' || '${inputId}' == 'single_proManager' ||'${inputId}' == "single_approver" || '${inputId}'.indexOf('single')>=0
 			  ||'${inputId}' == "single_softwareReceive" || '${inputId}' == "single_recorder" || '${inputId}' == "single_handlerUser" ||'${inputId}' == "single_recipient" || '${inputId}' == "single_materialManager"){
 				$("#selectUser").empty();
 				selectUserList.length=0;
 				managerIdSplit.length=0;
 				if(selectUserList.toString().indexOf(userId,userName)==-1){
 			 		selectUserList.push("{\"userId\":\""+userId+"\",\"userName\":\""+userName+"\",\"id\":\""+id+"\"}");
 			 		var html = "<p class=\"pick\" id='p_"+id+"'><input type='hidden' id='userId' name='userId' value='"+userId+"'/><input type='hidden' id='userName' name='userName' value='"+userName+"'/><input name='"+id+"_chk' id='"+id+"' value='"+userName+"' type='checkBox' onClick=\"delSelectUser('"+userId+"','"+userName+"','"+id+"',this);\">"+userName+"</p>";
 			 		$("#selectUser").append(html); 
 			    }
 				managerIdSplit.push(id);
 			}else{
				if(selectUserList.toString().indexOf(userId,userName)==-1){
			 		selectUserList.push("{\"userId\":\""+userId+"\",\"userName\":\""+userName+"\",\"id\":\""+id+"\"}");
			 		var html = "<p class=\"pick\" id='p_"+id+"'><input type='hidden' id='userId' name='userId' value='"+userId+"'/><input type='hidden' id='userName' name='userName' value='"+userName+"'/><input name='"+id+"_chk' id='"+id+"' value='"+userName+"' type='checkBox' onClick=\"delSelectUser('"+userId+"','"+userName+"','"+id+"',this);\">"+userName+"</p>";
			 		$("#selectUser").append(html); 
				} else{
					for(var i=0;i<selectUserList.length;i++){
			 			var obj = JSON.parse(selectUserList[i]);
			 			if(userId == obj.userId){
			 				$("#p_"+id).remove();
			 				//从当前坐标开始,向后删除一个
			 				selectUserList.splice(i,1);
			 			}
					}
			  } 
 			}
		} 
 	}
	
}
	function dj(o) {//添加选中事件
        var $this = $(o);
        if(o.className == 'pick'){
        	 $this.removeClass("pick");
        	 $this.siblings(".pick").removeClass("pick");
        	 $("#selectUser").empty();
        	 selectUserList.length=0;
        }else{
        	$this.addClass("pick");
            if($this.siblings(".pick").length>0){
                $this.siblings(".pick").removeClass("pick");
            };
        }
			
      
    }
	/* 点击右侧移除选中事件*/
 	function delSelectUser(userId,userName,id,o){
		$($("#"+id).parent()).removeClass("pick");
		for(var i=0;i<selectUserList.length;i++){
 			var obj = JSON.parse(selectUserList[i]);
 			if(id == obj.id){
 				$("#p_"+id).remove();
 				//从当前坐标开始,向后删除一个
 				selectUserList.splice(i,1);
 			}
 		}
		for(var j=0;j<managerIdSplit.length;j++){
 			if(id == managerIdSplit[j]){
 				managerIdSplit.splice(j,1);
 			}
 		}
	}
</script>
<body style="overflow: hidden;">
<div class="selectPersonnel">
	<div id="selectPersonnel1" style="width:170px;height:195px;">
	    <div id="main-left" class="zTreeDemoBackground main_left" style="border:0px;overflow-x: hidden;">
	    	<div class="selectPersonnel_div1" >组织结构</div>
	        <div class="left_menu_tree">
	            <ul id="treeDemo" class="ztree "></ul>
	        </div>
	    </div>
    </div> 
    <div id="main-right" class="main_right" style="overflow: hidden;">
        <div class="selectbox">
        	<div id="selectPersonnel2" style="width:315px;height:195px;overflow-y:auto;overflow-x: hidden;float:left;border-left:5px solid #ddd">
	            <div class="selectPersonnel_div2 fl">
	                <div class="selectPersonnel_div1">人员列表</div>
	                <div class="selectPersonnel_select1"  size='8' id="userList" style="width:295px;">
	                </div>
	            </div>
            </div>
            <div id="selectPersonnel3" style="width:144px;height:190px;overflow-y:auto;overflow-x: hidden;float:left;border-left:5px solid #ddd">
	            <div class="selectPersonnel_div4 fl" >
	                <div class="selectPersonnel_div1">已选列表</div>
	                <div class="selectPersonnel_select2" size='8' id="selectUser"></div>
	            </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
/* $(function(){
	$("#selectPersonnel1").mouseover(function(){
		  $("#selectPersonnel1").css("border","0px");
		});
	$("#selectPersonnel1").panel({iWheelStep:32});
});
$(function(){
	$("#selectPersonnel2").mouseover(function(){
		  $("#selectPersonnel2").css("border","0px");
		});
	$("#selectPersonnel2").panel({iWheelStep:32});
});
$(function(){
	$("#selectPersonnel3").panel({iWheelStep:32});
}); */

	function save(){
		var inputId='${inputId}';
		var stringJson = "["+selectUserList.join(",")+"]";
		//修改Bug[3328] [#3328] 程序错误，部门管理人不是必填项，但修改时无法取消勾选
		if(inputId == "charge" || inputId == "chargeInDeptAdd" || inputId == "project_manager" || inputId.indexOf('single')>=0){
		 	if(stringJson.indexOf("},{") == 0){
				layer.msg("请选择负责人");
				return;
			}if(stringJson.indexOf("},{") >= 0  && inputId == "project_manager"){
				layer.msg("只能选择一个项目负责人");
				return;
			}
		
		}
		if(inputId =="chargeInDeptAdd"){
			inputId = "charge";
		}
		parent.parent.setUserIdAndName(stringJson,inputId);
		parent.parent.closeWin();
	}
</script>
</html>